/**
 * Form Basics 
 *
 * Copyright (c) 2009 Thomas Filius
 * See the file LICENSE.txt for licensing information.
 */
var uri2mod = '/@jamak3/modules/kohadmin/';
var uri2app = '/@jamak3/index.php/';

function Product (id, name) {
	this.id = id;
	this.name = name;
	
	this.getHTML = function () {
		return "<option value="+ this.id +"> " + this.name + "</option>";
	}
}

function ProductList () {
	var plist = new Array("");
	
	return {
		length : function () {
			return plist.length;
		},
		
		add : function (k, v) {
			plist.push(new Product(k, v));
		},
		
		retrieveHTML : function () {
			t = "<option value=0>NEGERT!</option>";
			
			t = this.length();
			return t;
		}
	}
	
}
products = new ProductList();

function OrderItem (i, pid, q, p, t) {
	this.index = i;
	this.product_id = pid;
	this.quantity = q;
	this.price = p;
	this.total = t;
	
	this.returnHTML = function () {
		t = "<tr>";
		t+= "<td><img src='" + uri2mod + "assets/img/icon_delete.gif' /></td>";
		
		t+= "<td><select name='product_id[" + this.index  + "]' class='productlist' sel_index='" + this.product_id + "'>";
		t+= "<option value=0> </option>";
		t+= "</select></td>";
		
		t+= "<td><input type='text' name='quantity[" + this.index + "]' value='" + this.quantity + "' size=4 /></td>";
		t+= "<td><input type='text' name='price[" + this.index + "]' value='" + this.price + "' size=4 /></td>";
		t+= "<td><input type='text' name='total[" + this.index + "]' value='" + this.total + "' size=4 /></td>";
		
		t+= "</tr>";
		
		return t;
	}
}


function Order () {
	this.order_items = [];
	this.index = 0;
	
	this.add_order_item = function (pid, q, p, t) {
		this.order_items[this.index] = new OrderItem(this.index, pid, q, p, t);
		this.index++;
	};
	
	this.rebuild = function () {
		t = "";
		
		for (i=0; i<this.index; i++) {			
			t += this.order_items[i].returnHTML();
		}
		$("tbody.list_items").html(t);
	};	
}
order = new Order();


/**
 * Laden bij DOMready
 */
$( function () {
	$("#customer_id").change( function() {
		addOrder.getClientInfo($(this))
	});
	
	
	
	
		$.getJSON(uri2app + "products/get_all_products", function(data) {
			$.each(data, function(k,v) {
				products.add(k, v);					
			});				
		});
	
	console.log(products);
	console.log(products.retrieveHTML());
	$("select.productlist").html(products.retrieveHTML());
	//console.log();
	
	
	
	
});


//var list_items = {
//	items : new Array(),
//	products : new Array(""),
//	
//	add : function (product_id, quantity, price, total) {
//		this.items[this.items.length] = new Array(product_id, quantity, price, total);
//	},	
//	getItemHTML : function (id) {
//		
//		t = "<tr>";
//		t+= "<td><img src='" + uri2mod + "assets/img/icon_delete.gif' /></td>";
//		
//		t+= "<td><select name='product_id[" + id + "]'>";
//		t+= "</select></td>";
//		
//		t+= "<td><input type='text' name='quantity[" + id + "]' value='' size=4 /></td>";
//		t+= "<td><input type='text' name='price[" + id + "]' value='' size=4 /></td>";
//		t+= "<td><input type='text' name='total[" + id + "]' value='' size=4 /></td>";
//		
//		t+= "</tr>";
//		
//		return t;
//	},
//	
//	getProductOptions : function (id, product_id) {		
//		
//		if (this.products.length == 1) {
//			$.getJSON(uri2app + "products/get_options", function(data) {
//				$.each(data, function(k,v) {
//					list_items.products[k] = v;					
//				});				
//			});
//		}
//		
//		t = "";
//		
//		for (i=0; i<this.products.length; i++) {
//			t =+ "<option value=" + i + "> "+list_items.products[i] + "</option>";
//			
//		}
//		$("#product_id[" + id + "]").html(t);
//		
//	},
//	
//	fillItem : function (id, product_id, q, p, t) {
//		this.getProductOptions(id, product_id);
//		
//		$("#quantity[" + id + "]").html(q);
//	},
//	
//	rebuild : function () {
//		t = "";
//		for (i=0; i<this.items.length; i++)
//		{
//			t += list_items.getItemHTML(i);
//		}
//		$('tbody.list_items').html(t);
//		
//		for (i=0; i<this.items.length; i++)
//		{
//			alert(this.items[i]);
//			this.fillItem(i, this.items[i][0], this.items[i][1], this.items[i][2], this.items[i][3])
//		}
//		
//		this.getProductOptions();
//		alert(list_items.products.length);
//	}
//}
//
///**
// * Laden bij DOMready
// */
//$( function () {
//	
//	$("#customer_id").change( function() { addOrder.getClientInfo($(this)) });
//	$("a.add_item").click( function() { list_items.add(0,0,0,0); list_items.rebuild() });
//	
//	list_items.add(1, 3, 1, 3);
//	list_items.rebuild();
//
//});
//
//
///**
// * addOrder - prototype benodigd voor /orders/add
// */
//var addOrder = {
//	getClientInfo : function (e) {
//		$.getJSON(uri2app + "customers/view/"+e.val(), function(data)
//		{
//			$("#customer_debitnumber").html(data.debitnumber||"Niet beschikbaar");
//			$("#customer_address").html(data.address||"Niet beschikbaar");
//		});
//	},
//	
//	addListItem : function (e) {
//		alert('ja');
//		
//		t = "<tr>";
//		t += "<td></td>";
//		t += "<td><input type='text' /></td>";
//		
//		t += "</tr>";
//		e.html = e.html + t;
//	}
//	
//}


